<?php

namespace App\Admin\Controllers\Posts;

use App\Admin\Actions\Grid\PostPassAction;
use App\Admin\Actions\Grid\PostsAction;
use App\Admin\Actions\Grid\PostTopAction;
use App\Admin\Actions\Grid\PostUnTopAction;
use App\Admin\Actions\Grid\PostPushAction;
use App\Admin\Actions\Grid\PostShelvesAction;
use App\Admin\Repositories\Posts\WxPost;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Metrics\Card;

class WxPostController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxPost(['tags']), function (Grid $grid) {
            $grid->model()->orderBy('is_sticky', 'desc');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('is_sticky')->using([0 => '否', 1 => '↑'])->label([
                0 => 'white',
                1 => 'red'
            ])->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . $this->user['user_avatar'] . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user',)->display(function ($v) {
                return $v['user_name'];
            })->width('100px');
            $grid->column('posts_content')->display('更多') // 设置按钮名称
            ->expand(function ($a) {

                $content = $this->posts_content;
                $images = $this->images_url;
                $address = $this->address;
                $tagIds = WxPostsTag::where('posts_id', $this->id)->pluck('tags_id');
//                dump($tagIds);
                $tags = WxTag::whereIn('id', $tagIds)->get();
                $video = WxPostsVideo::where('posts_id', $this->id)->first();
                // 只填充内容，不需要标题
                $card = Card::make(view('admin/posts/detail', compact('content', 'images', 'address', 'tags', 'video')));
                return $card;

            })->width('150px');

//            $grid->column('images_url')->display(function ($pictures) {
//
//                return $pictures;
//
//            })->image('',200,90);
//            $grid->column('circle_id');

            $grid->column('circle_name');
//            $grid->column('tags')->display(function($v){
//                return $this->id;
//            });
//            $grid->column('address_id');
//            $grid->column('tags_id');
//            $grid->column('is_information')->select([0=>'否',1=>'是']);
            $grid->column('is_information')->switch()->sortable();
            $grid->column('is_examine')->using([0 => '待审核', 1 => '审核通过', 2 => '驳回'])->label([
                0 => 'red',
                1 => 'green',
                2 => 'default'
            ]);
//            $grid->column('overrule_content');
//            $grid->column('overrule_is_read');
            $grid->column('posts_state')->using([0 => '推送中', 1 => '用户删除', 2 => '已下架'])->label([
                0 => 'primary',
                1 => 'red',
                2 => 'default'
            ]);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('is_information')->select([0 => '否', 1 => '是']);
                $filter->equal('is_examine')->select([0 => '待审核', 1 => '审核通过', 2 => '驳回']);
                $filter->between('created_at')->date();
            });
            // 或
            $grid->batchActions(function ($batch) {
                $batch->add(new PostPassAction());
                $batch->add(new PostTopAction());
                $batch->add(new PostUnTopAction());
                $batch->add(new PostPushAction());
                $batch->add(new PostShelvesAction());

            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new PostsAction())->setKey($this->id));
            });

            $grid->disableCreateButton();
//            $grid->disableActions();
            // 禁用编辑按钮
            $grid->disableEditButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->disableBatchDelete();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxPost(), function (Show $show) {
            $show->field('id');
            $show->field('posts_content');
            $show->field('user_id');
            $show->field('circle_id');
            $show->field('circle_name');
            $show->field('address_id');
            $show->field('tags_id');
            $show->field('is_information');
            $show->field('is_examine');
            $show->field('overrule_content');
            $show->field('overrule_is_read');
            $show->field('posts_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxPost(), function (Form $form) {
            $form->display('id');
            $form->text('posts_content');
            $form->text('user_id');
            $form->text('circle_id');
            $form->text('circle_name');
            $form->text('address_id');
            $form->text('tags_id');
            $form->text('is_information');
            $form->text('is_examine');
            $form->text('overrule_content');
            $form->text('overrule_is_read');
            $form->text('posts_state');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
